package cn.hy.goods;

import cn.hy.redis.key.GoodsKey;
import cn.hy.service.SecKillGoodsService;
import cn.hy.vo.SecKillGoodsVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class GoodsWarmUp implements InitializingBean {

    @Resource
    private SecKillGoodsService goodsService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final Map<Long, Boolean> localOverMap = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {

        List<SecKillGoodsVo> list = goodsService.list();
        for (SecKillGoodsVo goods : list) {
            stringRedisTemplate.opsForValue().set(GoodsKey.GOODS_STOCK.getKey(String.valueOf(goods.getId())),
                    String.valueOf(goods.getStockCount()),GoodsKey.GOODS_STOCK.expireSeconds(), TimeUnit.SECONDS);
            localOverMap.put(goods.getId(), false);
        }
    }
}
